import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import qiankun from 'vite-plugin-qiankun';

export default defineConfig({
  plugins: [
    vue(),
    qiankun('login-app', {
      useDevMode: true,
    })
  ],
  css: {
    preprocessorOptions: {
      less: {
        additionalData: `@import "${resolve(__dirname, 'src/style/variables.less')}";`,
        javascriptEnabled: true
      }
    }
  },
  server: {
    port: 3003,
    host: '0.0.0.0',
    open: false,
    cors: true,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
      'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control',
    },
    fs: {
      allow: ['..', '../..', '../../..'],
    },
    proxy: {
      '/dev-api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path
      }
    }
  },
  base: './',
  optimizeDeps: {
    include: [
      'vue',
      'vue-router',
      'pinia',
      'element-plus',
      '@element-plus/icons-vue',
      '@tuxing/network-http',
      '@tuxing/core-token',
      '@tuxing/ui-components',
    ],
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    },
    dedupe: ['vue', 'vue-router', 'pinia', 'element-plus']
  },
  build: {
    lib: {
      entry: './src/main.ts',
      name: 'loginApp',
      formats: ['umd'],
      fileName: 'loginApp',
    },
    rollupOptions: {
      external: ['vue', 'vue-router', 'pinia'],
      output: {
        globals: {
          vue: 'Vue',
          'vue-router': 'VueRouter',
          pinia: 'Pinia',
        },
      },
    }
  }
});
